package cn.billy.pet.controller;

import cn.billy.basic.exception.BusinessException;
import cn.billy.basic.utils.LoginContext;
import cn.billy.org.domain.Employee;
import cn.billy.org.domain.Shop;
import cn.billy.org.service.IEmployeeService;
import cn.billy.org.service.IShopService;
import cn.billy.pet.dto.SearchMasterMsgDto;
import cn.billy.pet.service.ISearchMasterMsgService;
import cn.billy.pet.domain.SearchMasterMsg;
import cn.billy.pet.query.SearchMasterMsgQuery;
import cn.billy.basic.utils.AjaxResult;
import cn.billy.basic.utils.PageList;
import cn.billy.user.domain.Logininfo;
import cn.billy.user.domain.User;
import cn.billy.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IShopService shopService;
    @Autowired
    private IEmployeeService employeeService;
    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg getById(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.getById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> loadAll(){

        return searchMasterMsgService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> queryData(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryData(query);
    }

    // 寻主消息发布
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest req)
    {
        try {
            Logininfo logininfo = LoginContext.getLogininfo(req);
            User user = userService.getByLogininfoId(logininfo.getId());
            searchMasterMsg.setUserId(user.getId());
            searchMasterMsgService.publish(searchMasterMsg);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }
    /*/*
    * @Title: toAudit
    * @Description: 订单代审核
    * @Author: Billy
    * @Version: 1.0
    * @Date:  2022/7/20 12:51
    * @Parameters: [query]
    * @Return cn.billy.basic.utils.PageList<cn.billy.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query)
    {
        query.setState(0);
        return searchMasterMsgService.queryData(query);
    }

    /*/*
    * @Title: toHandle
    * @Description: 待处理订单
    * @Author: Billy
    * @Version: 1.0
    * @Date:  2022/7/20 12:58
    * @Parameters: [query, req]
    * @Return cn.billy.basic.utils.PageList<cn.billy.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest req)
    {
        query.setState(1);
        Logininfo logininfo = LoginContext.getLogininfo(req);
        Employee employee = employeeService.loadByLogininfoId(logininfo.getId());
        query.setShopId(employee.getShopId());
        return searchMasterMsgService.queryData(query);
    }
    /*/* 
    * @Title: pool
    * @Description: 没有shopid就寻主池
    * @Author: Billy
    * @Version: 1.0
    * @Date:  2022/7/20 17:02  
    * @Parameters: [query]
    * @Return cn.billy.basic.utils.PageList<cn.billy.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query)
    {
        query.setState(1);
        return searchMasterMsgService.queryData(query);
    }

    /*/* 
    * @Title: finish
    * @Description: 订单已完成
    * @Author: Billy
    * @Version: 1.0
    * @Date:  2022/7/20 17:04  
    * @Parameters: [query]
    * @Return cn.billy.basic.utils.PageList<cn.billy.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query)
    {
        query.setState(2);
        return searchMasterMsgService.queryData(query);
    }

    /*/* 
    * @Title: user
    * @Description: TODO
    * @Author: Billy
    * @Version: 1.0
    * @Date:  2022/7/20 18:06
    * @Parameters: [query, req]
    * @Return cn.billy.basic.utils.PageList<cn.billy.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest req)
    {
        query.setState(1);
        Logininfo logininfo = LoginContext.getLogininfo(req);
        User user = userService.getByLogininfoId(logininfo.getId());
        query.setShopId(user.getId());
        return searchMasterMsgService.queryData(query);
    }
    // accept
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody SearchMasterMsgDto dto)
    {
        try {
            searchMasterMsgService.accept(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    // 拒单接口
    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId){
        try {
            searchMasterMsgService.reject(msgId);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
